package com.tongzhou.tongzhou.quot.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.tongzhou.db.DBUtil;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.quot.dao.IPaperTypeDAO;
import com.tongzhou.tongzhou.quot.model.PaperType;
import com.tongzhou.tongzhou.quot.model.SearchPaperTypeCondition;
import com.tongzhou.util.Util;

public class PaperTypeDAO implements IPaperTypeDAO{

	@Override
	public int addPaperType(PaperType paperType) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("insert into paper_type(id,paper_type_name,account_id,region_id) ");
		sql.append("values('"+Util.changeToObj(paperType.getId())+"',");
		sql.append("'"+Util.changeToObj(paperType.getPaper_type_name())+"',");
		sql.append("'"+paperType.getAccount_id()+"',");
		sql.append("'"+paperType.getRegion_id()+"')");
		int result=DBUtil.update(sql.toString());
		return result;
	}

	@Override
	public void deletePaperType(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("delete from paper_type where id='"+id+"'");
		DBUtil.update(sql.toString());
	}

	@Override
	public PaperType getPaperTypeById(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select  id,paper_type_name ");
		sql.append(" from paper_type ");
		sql.append(" where id='"+id+"'");
		PaperType paperType=DBUtil.query(sql.toString(), new BeanHandler<PaperType>(PaperType.class));
		return paperType;
	}

	@Override
	public List<PaperType> listPaperType(SearchPaperTypeCondition condition)
			throws DBException {
		List<PaperType> result=null;
		
		//查询条件SQL
		//查询参数
		StringBuffer sqlCondition = new StringBuffer() ;
		List<String> paramList = new ArrayList<String>() ;
		
		//取得满足条件的总记录数和总页数
		StringBuffer sql = new StringBuffer() ;
		sql.append("select count(id) ");
		sql.append(" from paper_type ");
		sql.append(" where 1=1 ");
		
		//总记录数
		long total = DBUtil.getCount(sql.toString(),paramList.toArray()) ;
		//总页数
		long totalPage = Util.getTotalPage(total, condition.getPageSize());
		
		condition.setTotal(total);
		condition.setTotalPage(totalPage);
		
		//查询某页的列表数据
		sql.delete(0, sql.length());
		sql.append("select  id,paper_type_name ");
		sql.append(" from paper_type ");
		sql.append(" where 1=1 ");
		sql.append(" order by "+condition.getOrderBy()+" "+condition.getOrderByType()) ;
		result = DBUtil.query(sql.toString(), new BeanListHandler<PaperType>(PaperType.class), paramList.toArray(), Util.getStartPos(condition.getCurrentPage(), condition.getPageSize()), condition.getPageSize()) ;
		return result;
	}

	@Override
	public void updatePaperType(PaperType paperType) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("update paper_type set ");
		sql.append("paper_type_name='"+Util.changeToObj(paperType.getPaper_type_name())+"' ");
		sql.append("where id='"+paperType.getId()+"'");
		DBUtil.update(sql.toString());
	}
	
	@Override
	public List<PaperType> getPaperTypeList() throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select p.id,p.paper_type_name from paper_type p ");
		sql.append(" where 1=1 ");
		List<PaperType> result=DBUtil.query(sql.toString(),new BeanListHandler<PaperType>(PaperType.class));
		return result;
	}

}
